package com.wonders.qxmanager.controller;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wonders.qxmanager.model.Role;
import com.wonders.qxmanager.model.User;
import com.wonders.qxmanager.service.RoleService;
import com.wonders.qxmanager.utils.PageArrayList;
import com.wonders.qxmanager.utils.PageUIHelper;
import com.wonders.qxmanager.utils.PageUIHelper.Page;

/**
 * Role web控制器
 * @author 
 *
 */
@Controller
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(value = "/pages/role/save.do", method = RequestMethod.POST)
	public void save(Role role, HttpServletRequest req, HttpServletResponse resp) throws Exception{
		roleService.addRole(role);
		PageUIHelper.writeFormResponse(resp, "{errorMsg:true}");
	}
	@RequestMapping("/pages/role/delete.do")
	public void remove(@RequestParam("delId")int id,HttpServletRequest req, HttpServletResponse resp) throws Exception {
		roleService.deleteRole(new BigDecimal(id));
		System.out.println("删除成功");
		PageUIHelper.writeFormResponse(resp, "{errorMsg:true}");
	}

	@RequestMapping("/pages/role/edit.do")
	public void edit(Role role, HttpServletRequest req, HttpServletResponse resp) throws Exception{
		roleService.updateRole(role);
		PageUIHelper.writeFormResponse(resp, "{errorMsg:true}");
	}

	@RequestMapping("/pages/role/list.do")
	public void list(Role role, HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		Page page = PageUIHelper.getPage(req);
		PageArrayList<Role> list = roleService.getListByPage(role, page);
		PageUIHelper.writeJsonResponse(resp, PageUIHelper.toString(list));
	}
	
	public ModelAndView getRoleById(@RequestParam("id")int id,HttpServletRequest req, HttpServletResponse resp){
		req.setAttribute("role",roleService.getRoleById(new BigDecimal(id)));
		return new ModelAndView("pages/role/c");
	}
	
}
